package com.sjm.bookshop.controller.logincontroller;

import com.sjm.bookshop.entity.Admin;
import com.sjm.bookshop.entity.User;
import com.sjm.bookshop.service.userloginservice.LoginService;
import com.sjm.bookshop.utils.JwtUtils;
import com.sjm.bookshop.utils.MD5Util;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher; //正则表达式匹配器
import java.util.regex.Pattern; //正则表达式的对象


@RestController
@RequestMapping("/login")
public class UserLogin {
    @Autowired
    private LoginService loginService;
    @Autowired
    private RedisTemplate redisTemplate;

    //  用户登录
    @RequestMapping("/userLogin")
    public Result userLogin(String phone, String password) {
        if (phone != null && password != null) {
//            System.out.println("---->"+MD5Util.MD5(password));
            User user = loginService.login(phone, MD5Util.MD5(password));
            if (user != null && user.getIsLocked().equals(0)) {
                String token = JwtUtils.createToken(user);
                return Result.success(200, user, "登录成功", token);
            } else {
                return Result.fail(201, "账号或密码错误");
            }
        } else {
            return Result.fail(202, "账号或密码不能为空");
        }
    }

    @RequestMapping("/userRegister")
    public Result userRegister(@RequestBody User user) {
        if (user == null) {
            return Result.fail(201, "提交信息有误");
        } else {
            int res = loginService.register(user);
            if (res > 0) {
                return Result.success(200, "注册成功");
            } else {
                return Result.fail(201, "手机号已被注册");
            }
        }

    }

    //    管理员登录
    @RequestMapping("/adminLogin")
    public Result adminLogin(String phone, String password) {
        if (phone != null && password != null) {
            Admin admin = loginService.admin(phone, MD5Util.MD5(password));
            if (admin != null && admin.getAdminstatus() == 0) {
                String token = JwtUtils.createToken(admin);
                System.out.println(token);
                return Result.success(200, admin, "登录成功", token);
            } else {
                return Result.fail(201, "账号或密码错误,请联系管理员");
            }
        } else {
            return Result.fail(202, "账号密码不能为空");
        }
    }

    //    管理员注册
    @RequestMapping("/register")
    public Result RegisterAdminInfo(@RequestBody Admin admin) {
        int result = loginService.addAdmin(admin);
        if (result > 0) {
            return Result.success(200, "注册成功");
        } else {
            return Result.success(201, "注册失败");
        }
    }

    /**
     * 找回密码
     */
    @RequestMapping("/forgot")
    public Result RegisterAdminInfo(String phone, String email, String password, String code) {
        ValueOperations ops = redisTemplate.opsForValue();
        String countEmailKey = "VerifyEmail:" + email + ":count";//邮箱key
        String codeKey = "VerifyCode:" + email + ":code";//验证码key
        Object o1 = ops.get(countEmailKey);
        int i = Integer.parseInt(o1.toString());
        Object o2 = ops.get(codeKey);
        if (redisTemplate.hasKey(codeKey)&&(i > 0 && i <= 3)&&(o2.toString().equals(code))) {
            int res = loginService.resetPwd(phone, MD5Util.MD5(password));
            if (res > 0)
                return Result.success(200, "修改成功");
            else
                return Result.fail(201, "重置失败");
        } else {
            return Result.success(201, "验证码过期");
        }
    }


}
